import React, { useState } from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import publicRoutes from './routes/publicRoutes';
import adminRoutes from './routes/adminRoutes'; // 如果有 adminRoutes
import privateRoutes from './routes/privateRoutes';
import AuthRoute from './components/AuthRoute';

function App() {
  const [user, setUser] = useState<any>({});

  const loginAsUser = () => {
    setUser({ role: ['user'] });
  }

  const loginAsAdmin = () => {
    setUser({ role: ['user', 'admin'] });
  }

  return (
    <Router>
      <Routes>
        {publicRoutes.map((route) => (
          <Route
            key={route.path}
            path={route.path}
            element={<route.component loginAsUser={loginAsUser} loginAsAdmin={loginAsAdmin} />}
          />
        ))}
        {privateRoutes.map((route) => (
          <Route
            key={route.path}
            path={route.path}
            element={
              <AuthRoute
                user={user}
                role={route.role}
                backUrl={route.backUrl}
                component={route.component}
              />
            }
          />
        ))}
        {adminRoutes.map((route) => (
          <Route
            key={route.path}
            path={route.path}
            element={
              <AuthRoute
                user={user}
                role={route.role}
                backUrl={route.backUrl}
                component={route.component}
              />
            }
          />
        ))}
      </Routes>
    </Router>
  );
}

export default App;
